package com.mofang.powerdekorhelper.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.mofang.powerdekorhelper.R;
import com.mofang.powerdekorhelper.activity.login.LoginActivity;
import com.mofang.powerdekorhelper.activity.mine.AboutActivity;
import com.mofang.powerdekorhelper.activity.mine.BindPhoneVerifyActivity;
import com.mofang.powerdekorhelper.activity.mine.MyInvalidataActivity;
import com.mofang.powerdekorhelper.base.MvpFragment;
import com.mofang.powerdekorhelper.persenter.PersonalPersenter;
import com.mofang.powerdekorhelper.utils.ConnectionUtils;
import com.mofang.powerdekorhelper.utils.DialogUtils;
import com.mofang.powerdekorhelper.utils.SharePerforenceUtils;
import com.mofang.powerdekorhelper.utils.T;
import com.mofang.powerdekorhelper.view.PersonalView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * Created by luofei on 2017/8/16 0016.
 * 个人中心
 */

public class PersonalCenterFragment extends MvpFragment<PersonalView, PersonalPersenter> implements PersonalView {

    public final static int toBindPhoneVerifyCode = 111;

    @BindView(R.id.user_account_tv)
    TextView accountTv;

    @BindView(R.id.binding_phone_tv)
    TextView bindPhoneTv;

    @BindView(R.id.company_tv)
    TextView companyTv;

    @BindView(R.id.work_plant_brand_nme_tv)
    TextView nameTv;

    public static PersonalCenterFragment fragment;

    public static PersonalCenterFragment getInstance() {
        if (fragment == null) {
            fragment = new PersonalCenterFragment();
        }
        return fragment;
    }

    @Override
    public View initView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.fragment_personal_center_layout, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void initData(Bundle savedInstanceState) {

        String shopName = SharePerforenceUtils.getInstance(mContext).getBelongCompany();

        companyTv.setText(shopName);

        String account = SharePerforenceUtils.getInstance(mContext).getUserName();
        accountTv.setText(account);

        String name = SharePerforenceUtils.getInstance(mContext).getName();
        nameTv.setText(name);

        String bindphone = SharePerforenceUtils.getInstance(mContext).getPhone();

        if (!bindphone.equals("")) {
            String targetStr = bindphone.substring(3, 7);
            bindphone = bindphone.replace(targetStr, "****");
            bindPhoneTv.setText(bindphone);
        } else {
            bindPhoneTv.setText("暂未绑定手机号");
        }
    }

    @OnClick({R.id.change_account_continer, R.id.binding_phone_continer, R.id.invitation_code_continer, R.id.about_continer, R.id.personal_user_exit})
    void onclick(View view) {
        switch (view.getId()) {

            /*case R.id.change_account_continer:
                toActivity(ChangeAccountActivity.class);
                break;*/

            case R.id.binding_phone_continer:
                // 绑定手机
                startActivityForResult(new Intent(mContext, BindPhoneVerifyActivity.class), toBindPhoneVerifyCode);
                break;

            case R.id.invitation_code_continer:

                toActivity(MyInvalidataActivity.class);

                break;

            case R.id.about_continer:

                toActivity(AboutActivity.class);
                break;

            case R.id.personal_user_exit:

                DialogUtils exitDialog = DialogUtils.getInstance();
                exitDialog.showCustomDialog(mContext, "提示", "确定退出登录", "退出登录", "   取  消   ", new MySweetAlertDialogSureListener(), new MySweetAlertDialogCancleListener());
                break;
        }
    }

    class MySweetAlertDialogSureListener implements SweetAlertDialog.OnSweetClickListener {
        @Override
        public void onClick(SweetAlertDialog sweetAlertDialog) {
            int userId = SharePerforenceUtils.getInstance(mContext).getUserID();
            //presenter.getLogout(userId);
            logoutEmclinet();
            toast("请重新登录");
            SharePerforenceUtils.getInstance(mContext).setLogin(false);
            SharePerforenceUtils.getInstance(mContext).setUserName("");
            SharePerforenceUtils.getInstance(mContext).setUserID(-1);
            SharePerforenceUtils.getInstance(mContext).setPhone("");
            toActivity(LoginActivity.class);
            getActivity().finish();

            sweetAlertDialog.dismiss();
        }
    }

    class MySweetAlertDialogCancleListener implements SweetAlertDialog.OnSweetClickListener {
        @Override
        public void onClick(SweetAlertDialog sweetAlertDialog) {
            sweetAlertDialog.dismiss();
        }
    }

    public void logoutEmclinet() {
        // 退出环信
        ConnectionUtils connectionUtils = ConnectionUtils.getInstance();
        connectionUtils.exit();
    }

    @Override
    public void setLogout(String info) {

        if (info != null && !info.equals("")) {
            if (info.contains("成功")) {
                toast("请重新登录");
                SharePerforenceUtils.getInstance(mContext).setLogin(false);
                SharePerforenceUtils.getInstance(mContext).setUserName("");
                SharePerforenceUtils.getInstance(mContext).setUserID(-1);
                SharePerforenceUtils.getInstance(mContext).setPhone("");
                toActivity(LoginActivity.class);
                mContext.finish();

            } else {
                toast("退出登录失败");
            }
        } else {
            toast("退出登录失败");
        }
    }

    @Override
    public PersonalPersenter initPresenter() {
        return new PersonalPersenter();
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void toast(CharSequence s) {
        T.showShort(mContext, s);
    }

    @Override
    public void toast(int id) {

    }

    @Override
    public void showNullLayout() {

    }

    @Override
    public void hideNullLayout() {

    }

    @Override
    public void showErrorLayout(View.OnClickListener listener) {

    }

    @Override
    public void hideErrorLayout() {

    }

    @Override
    public void onError(String errorInfo) {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == toBindPhoneVerifyCode) {

            String bindphone = SharePerforenceUtils.getInstance(mContext).getPhone();

            if (!bindphone.equals("")) {
                String targetStr = bindphone.substring(3, 7);
                bindphone = bindphone.replace(targetStr, "****");
                bindPhoneTv.setText(bindphone);
            } else {
                bindPhoneTv.setText("暂未绑定手机号");
            }
        }
    }
}
